home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / SLAX 6.0.8 / slax-6.0.8.iso / slax / base / 006-devel.lzm / usr / include / ksavefile.h < prev    next >
Encoding:
C/C++ Source or Header  |  2005-10-10  |  4.6 KB  |  153 lines

  1. /*
  2.    This file is part of the KDE libraries
  3.    Copyright (c) 1999 Waldo Bastian <bastian@kde.org>
  4.  
  5.    This library is free software; you can redistribute it and/or
  6.    modify it under the terms of the GNU Library General Public
  7.    License version 2 as published by the Free Software Foundation.
  8.  
  9.    This library is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12.    Library General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU Library General Public License
  15.    along with this library; see the file COPYING.LIB.  If not, write to
  16.    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  17.    Boston, MA 02110-1301, USA.
  18. */
  19.  
  20. #ifndef _KSAVEFILE_H_
  21. #define _KSAVEFILE_H_
  22.  
  23. #include <qstring.h>
  24. #include <stdio.h>
  25. #include <errno.h>
  26. #include <ktempfile.h>
  27.  
  28. class KSaveFilePrivate;
  29.  
  30. /**
  31.  * The KSaveFile class has been made to write out changes to an existing
  32.  * file atomically.
  33.  * This means that EITHER:
  34.  * a)
  35.  *   All changes have been written successfully to the file.
  36.  *
  37.  * b)
  38.  *   Some error occurred, no changes have been written whatsoever and the
  39.  *   old file is still in place.
  40.  */
  41. class KDECORE_EXPORT KSaveFile
  42. {
  43. public:
  44.    /**
  45.     * Creates a new KSaveFile with the given file name.
  46.     * @param filename the path of the file
  47.     * @param mode the mode of the file (see chmod(1))
  48.     */
  49.    KSaveFile(const QString &filename, int mode = 0666 );
  50.  
  51.    /**
  52.     * The destructor closes the file.
  53.     * You might want to call close() explicitely though, to test whether it worked.
  54.     **/
  55.    ~KSaveFile();
  56.  
  57.    /**
  58.     * Returns the status of the file based on errno. (see errno.h)
  59.     * 0 means OK.
  60.     *
  61.     * You should check the status after object creation to check
  62.     * whether a file could be created in the first place.
  63.     *
  64.     * You may check the status after closing the file to verify that
  65.     * the file has indeed been written correctly.
  66.     * @return the errno status, 0 means ok
  67.     **/
  68.    int status() const
  69.        { return mTempFile.status(); }
  70.  
  71.    /**
  72.     * The name of the file as passed to the constructor.
  73.     * @return The name of the file, or QString::null if opening the
  74.     *         file has failed
  75.     **/
  76.    QString name() const;
  77.  
  78.    /**
  79.     * An integer file descriptor open for writing to the file.
  80.     * @return The file descriptor, or a negative number if opening
  81.     *         the temporary file failed
  82.     **/
  83.    int handle()    const
  84.        { return mTempFile.handle(); }
  85.  
  86.    /**
  87.     * A FILE* stream open for writing to the file.
  88.     * @return FILE* stream open for writing to the file, or 0
  89.     *         if opening the temporary file failed
  90.     **/
  91.    FILE *fstream()
  92.        { return mTempFile.fstream(); }
  93.  
  94.    /**
  95.     * A QFile* open for writing to the file.
  96.     * @return A QFile open for writing to the file, or 0 if
  97.     *         opening the temporary file failed.
  98.     **/
  99.    QFile *file()
  100.        { return mTempFile.file(); }
  101.  
  102.    /**
  103.     * A QTextStream* open for writing to the file.
  104.     * @return A QTextStream that is open for writing to the file, or 0
  105.     *         if opening the temporary file failed
  106.     **/
  107.    QTextStream *textStream()
  108.        { return mTempFile.textStream(); }
  109.  
  110.    /**
  111.     * A QDataStream* open for writing to the file.
  112.     * @return A QDataStream that is open for writing to the file, or 0
  113.     *         if opening the file failed
  114.     **/
  115.    QDataStream *dataStream()
  116.        { return mTempFile.dataStream(); }
  117.  
  118.    /**
  119.     * Aborts the write operation and removes any intermediate files
  120.     * This implies a close.
  121.     **/
  122.    void abort();
  123.  
  124.    /**
  125.     * Closes the file and makes the changes definitive.
  126.     * Returns 'true' is successful, or 'false' if an error has occurred.
  127.     * See status() for details about errors.
  128.     * @return true if successful, or false if an error has occurred.
  129.     **/
  130.    bool close();
  131.  
  132.     /**
  133.      * Static method to create a backup file before saving.
  134.      * You can use this method even if you don't use KSaveFile.
  135.      * @param filename the file to backup
  136.      * @param backupDir optional directory where to save the backup file in.
  137.      * If empty (the default), the backup will be in the same directory as @p filename.
  138.      * @param backupExtension the extension to append to @p filename, "~" by default.
  139.      * @since 3.2
  140.      */
  141.    static bool backupFile( const QString& filename,
  142.                            const QString& backupDir = QString::null,
  143.                            const QString& backupExtension = QString::fromLatin1( "~" ) );
  144.  
  145. private:
  146.    QString mFileName;
  147.    KTempFile mTempFile;
  148.  
  149.    KSaveFilePrivate *d;
  150. };
  151.  
  152. #endif
  153.